package com.acme.bankapp.service.bank;

import java.util.ArrayList;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;

public class BankService {
	public Bank bank;

	private final static String savingAccount = "Saving account: ";
	private final static String checkingAccount = " Checking account: ";

	public BankService() {
		ArrayList<ClientRegistrationListener> arrayList = new ArrayList<ClientRegistrationListener>();
		arrayList.add(new PrintClientListener());
		arrayList.add(new EmailNotificationListener());

		this.bank = new Bank(arrayList);
	}

	public void addClient(Client client) {
		bank.clients.add(client);
		for (ClientRegistrationListener listener : bank.listeners) {
			listener.onClientAdded(client);
		}

	}

	public void modifyBank(Client client, double amount, boolean meth) {
		if (meth == true) {
			client.accountS.deposit(amount);
		} else {
			client.accountS.withdraw(amount);
		}
	}

	public void printBalance() {
		for (Client client : bank.clients) {
			if (client == null) {
				break;
			}
			System.out.println(client.accountS.getBalance());
		}
	}

	public void printMaximumAmountToWidthdraw(Client client) {
		System.out.print(savingAccount
				+ client.accountS.maximumAmountToWithdraw());
		System.out.println(checkingAccount
				+ client.accountC.maximumAmountToWithdraw());
	}
	
	
	
	private class EmailNotificationListener implements ClientRegistrationListener {
		private final static String before = "Notification email for client ";
		private final static String after = " to be sent";

		@Override
		public void onClientAdded(Client client) {
			System.out.println(before + client.getName() + after);
		}

	}
	
	
	private class PrintClientListener implements ClientRegistrationListener {

		@Override
		public void onClientAdded(Client client) {
			System.out.println(client.getClientSalutation());
		}

	}
	
}
